package com.kz.himalaya.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.kz.himalaya.R;
import com.kz.himalaya.databinding.ActivityMainBinding;
import com.kz.himalaya.fragment.DiscoveryFragment;
import com.kz.himalaya.fragment.HomePageFragment;
import com.kz.himalaya.fragment.MyFragment;
import com.kz.himalaya.fragment.SubscribeFragment;
import com.kz.himalaya.service.PlayService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private int oldPosition = 0;
    private Fragment[] fragments = new Fragment[]{HomePageFragment.newInstance(), SubscribeFragment.newInstance()
            , DiscoveryFragment.newInstance(), MyFragment.newInstance()};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.rb1.setOnClickListener(this);
        binding.rb2.setOnClickListener(this);
        binding.rb3.setOnClickListener(this);
        binding.rb4.setOnClickListener(this);
        binding.playBtn.setOnClickListener(this);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_box, fragments[0])
                .commit();
        addFgt(0);
    }


    private void addFgt(int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (oldPosition == position) {
//            transaction.replace(R.id.fragment_box,fragments[position]).commit();
            return;
        }
        transaction.hide(fragments[oldPosition]);
        if (fragments[position].isAdded()) {
            transaction.show(fragments[position]);
        } else {
            transaction.add(R.id.fragment_box, fragments[position]).show(fragments[position]);
        }
        oldPosition = position;
        transaction.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rb1:
                addFgt(0);
                break;
            case R.id.rb2:
                addFgt(1);
                break;
            case R.id.rb3:
                addFgt(2);
                break;
            case R.id.rb4:
                addFgt(3);
                break;
            case R.id.play_btn:
                Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(this, PlayService.class);
        stopService(intent);
    }

}
